//
//  WeeklyViewController.m
//  LeagueOfLegend
//
//  Created by appying_wxj on 14/12/23.
//  Copyright (c) 2014年 appying_wxj. All rights reserved.
//

#import "WeeklyViewController.h"

@interface WeeklyViewController ()

@end

@implementation WeeklyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initWeeklyUI];
    [self getweeklyData];
}
// 创建控件
-(void)initWeeklyUI{
    self.titleLab.text = @"本周免费英雄";
    
    //显示时间的模块
    UILabel *timeLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 64.5, 320, 20)];
    NSDate * senddate = [NSDate date];
    NSCalendar * cal = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit;
    NSDateComponents * component = [cal components:unitFlags fromDate:senddate];
    NSInteger year = [component year];
    NSInteger month = [component month];
    NSInteger day = [component day];
    //NSLog(@"%4d/%2d/%2d",year,month,day);
    NSString *times = [NSString stringWithFormat:@"%d年%d月%d日",year,month,day];
    timeLab.text = times;
    timeLab.textAlignment = NSTextAlignmentCenter;// 居中设置
    timeLab.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:timeLab];
    
    
    
    _weeklyTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64-49)];
    _weeklyTableView.backgroundColor = [UIColor whiteColor];
    _weeklyTableView.separatorStyle = UITableViewCellAccessoryNone;
    _weeklyTableView.rowHeight = 110;
    _weeklyTableView.delegate = self;
    _weeklyTableView.dataSource = self;
    [self.view addSubview:_weeklyTableView];
    
    if (version>=7){
        timeLab.frame = CGRectMake(0, 64.5, self.view.frame.size.width, 20);
         _weeklyTableView.frame =  CGRectMake(0, 84.5, 320, SCREEN_HEIGHT-64-49-20);
        
    }else{
        timeLab.frame = CGRectMake(0, 24.5, self.view.frame.size.width, 20);
        _weeklyTableView.frame =  CGRectMake(0, 44.5, 320, SCREEN_HEIGHT-44-49-20);
    }
}
// 获取数据
-(void)getweeklyData{
    _imageArray = [[NSMutableArray alloc] initWithCapacity:0];
    _titleArray = [[NSMutableArray alloc] initWithCapacity:0];
    NSString *reqStr = [NSString stringWithFormat:@"http://lol.data.shiwan.com/lolHeros/?filter=&type=free"];
    NSString *getStr = [Request requestData:reqStr];
    NSDictionary *bigDic = [Request JsonDatas:getStr];
    _bigArr=[bigDic objectForKey:@"result"];
    for (int i = 0 ; i < _bigArr.count ; i++) {
        [_imageArray addObject:[[_bigArr objectAtIndex:i] objectForKey:@"img"]];
        [_titleArray addObject:[[_bigArr objectAtIndex:i] objectForKey:@"name_c"]];
    }
}
#pragma mark-------------UITableView  dataSource  delegate----------------
//返回多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _bigArr.count/3+1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static  NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    else{
        while ([cell.contentView.subviews lastObject] != nil) {
            [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row<_bigArr.count) {
        
        //_bigArr = 13
        int V = _bigArr.count / 3 + 1;
        
        for (int i=0; (indexPath.row < V - 1) ? i < 3 : i < _bigArr.count % 3; i++)
        {
            // 请求出来的图片的大小是 64*64
            
            UIImageView *heroImage = [[UIImageView alloc] initWithFrame:CGRectMake(16+i%3*(85+15), 8, 85, 85)];
            [heroImage setImageWithURL:[NSURL URLWithString:[_imageArray objectAtIndex:(3 * indexPath.row + i)]] placeholderImage:nil];
            [cell.contentView addSubview:heroImage];
            
            
            UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(16+i%3*(85+15), 95, 85, 15)];
            titleLab.text = [_titleArray objectAtIndex:(3 * indexPath.row + i)];
            titleLab.textAlignment = NSTextAlignmentCenter;
            titleLab.font = [UIFont systemFontOfSize:12];
            titleLab.textColor = [UIColor blackColor];
            [cell.contentView addSubview:titleLab];
        
        }
    
    }
   
//    cell.textLabel.text = [_titleArray objectAtIndex:indexPath.row];
    return cell;
}
// 点击单元格的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
